package productconsume;

import java.util.concurrent.TimeUnit;

/**
 * @Author ufo
 * @Description TODO
 * @Date 2021/1/20 14:35
 * @Version 1.0
 */
public class CallAndEmail {
    public static void main(String[] args) {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        Phone phone3 = new Phone();


        new Thread(() -> {
            phone1.staticsycnyanshi();
        }, "A1").start();

        new Thread(() -> {
            phone1.staticsycn();
        }, "A11").start();

        new Thread(() -> {
            phone1.sendEmail();
        }, "A2").start();


        new Thread(() -> {
            phone1.call();
        }, "A3").start();


        new Thread(() -> {
            phone2.call();
        }, "B1").start();

        new Thread(() -> {
            phone2.staticsycnyanshi();
        }, "B2").start();

    }
}


class Phone {

    //    synchronized锁的是生成的对象，就是上面生成的对象
    public synchronized void sendEmail() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "->发邮件（对象锁）");
    }

    public synchronized void call() {
        System.out.println(Thread.currentThread().getName() + "->打电话（对象锁）");

    }

    public void sayhello() {
        System.out.println(Thread.currentThread().getName() + "->我没有锁");

    }
    //    synchronized锁的是CLASS模板,锁得不一样就互不影响，CLASS模板锁不会影响对象锁
    public static synchronized void staticsycnyanshi() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "->静态锁，锁class模板");
    }

    public static synchronized void staticsycn() {

        System.out.println(Thread.currentThread().getName() + "->静态锁，无延时，锁class模板");
    }



}
